package com.suda.controller.admin;

import com.suda.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 不之味
 */
@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {

    @Autowired
    private RedisTemplate redisTemplate;

    public static final String KEY = "SHOP_STATUS";

    /**
     * 设置店铺营业状态
     *
     * @param status
     * @return
     */
    @PutMapping("/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result setStatus(@PathVariable Integer status) {
        if (status != 0 && status != 1) {
            log.warn("非法的店铺状态值: {}", status);
        }
        try {
            log.info("设置店铺营业状态为：{}", status == 1 ? "营业中" : "打烊中");
            redisTemplate.opsForValue().set(KEY, status);
        } catch (Exception e) {
            log.error("设置店铺状态失败: {}", e.getMessage(), e);

        }
        return Result.success();
    }

    @GetMapping("/status")
    @ApiOperation("获取店铺营业状态")
    public Result<Integer> getStatus() {
        try {
            Integer status = (Integer) redisTemplate.opsForValue().get(KEY);
            log.info("获取店铺的营业状态为 : {}", status == 1 ? "营业中" : "打烊中");
            return Result.success(status);
        } catch (Exception e) {
            log.error("获取店铺状态失败: {}", e.getMessage(), e);
        }
        return null;
    }
}
